<?php
/**
 * 用户管理模块
 * @author sundongdong
 * @date 2016-07-06
 */
class System_UserController extends Controller_NeedLogin {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init();
		$this->userModel = new System_Model_User();
	}
	/**
	 * 用户列表
	 */
	public function indexAction() {
		$getData = $this->getRequest()->getParams();

		$params['keyword'] = !empty($getData['keyword']) ? trim($getData['keyword']) : '';
		$params['dept_id'] = !empty($getData['dept_id']) ? trim($getData['dept_id']) : '';
		$params['page'] = !empty($getData['page']) ? $getData['page'] : 1;
		$params['perPage'] = isset($getData['page_size']) ? $getData['page_size'] : $this->page_size;
		$params['role_id'] = !empty($getData['role_id']) ? $getData['role_id'] : '';
		$params['cat_id'] = !empty($getData['cat_id']) ? trim($getData['cat_id']) : '';
		$params['select_cat_id'] = !empty($getData['select_cat_id']) ? trim($getData['select_cat_id']) : '';
		$params['select_dept_id'] = isset($getData['select_dept_id']) ? intval($getData['select_dept_id']) : 0;
		$params['is_group_leader'] = isset($getData['is_group_leader']) && in_array($getData['is_group_leader'], array(0, 1)) ? $getData['is_group_leader'] : '';
		$params['is_enable'] = isset($getData['is_enable']) && in_array($getData['is_enable'], array(0, 1)) ? $getData['is_enable'] : '';
		$params['deval'] = $this->userModel->getCategoryParents($params['select_cat_id']);
		$params['dept_path'] = $this->userModel->getDepartmentParents($params['select_dept_id']);

		$data = $this->userModel->getUserList($params);
		$page_link = $this->page($data['totalCounts'], $params['perPage']);

		$roles = $this->userModel->getAllRoles();

		$this->smarty->assign('roles', $roles);
		$this->smarty->assign('params', $params);
		$this->smarty->assign('page_link', $page_link);
		$this->smarty->assign('users', $data['list']);
		$this->smarty->display('sys_user.tpl');
	}
	/**
	 * 设置用户权限
	 */
	public function setprivilegeAction() {
		$getData = $this->getRequest()->getParams();
		$set_user_id = $getData['id'];
		$user_info = $this->userModel->getUserinfo($set_user_id);
		// 角色权限
		$allRole = $this->userModel->getAllRoles();
		$userRole = $this->userModel->getUserRoles($set_user_id);
		$newUserRole = array();
		if ($userRole) {
			foreach ($userRole as $ur) {
				$newUserRole[$ur['role_id']] = 1;
			}
		}
		// 分类权限
		$allCatesInfo = $this->userModel->getAllCategorys('name_ch', false);
		$userCatesInfo = $this->userModel->getUserCates($set_user_id);
		$userCatesInfo_new = array();
		if ($userCatesInfo) {
			foreach ($userCatesInfo as $userCate) {
				$userCatesInfo_new[] = $userCate['category_id'];
			}
		}
		if ($allCatesInfo) {
			foreach ($allCatesInfo as $cateKey => $cateInfo) {
				if (in_array($cateInfo['id'], $userCatesInfo_new)) {
					$allCatesInfo[$cateKey]['checked'] = 'true';
				}
			}
		}
		$cate_json = json_encode($allCatesInfo);
		// 仓库权限
		$allWarehouses = $this->userModel->getAllWares('warehouse_name', false);
		$userWareInfo = $this->userModel->getUserWares($set_user_id);
		$userWareInfo_new = array();
		if ($userWareInfo) {
			foreach ($userWareInfo as $userWare) {
				$userWareInfo_new[] = $userWare['warehouse_id'];
			}
		}
		if ($allWarehouses) {
			foreach ($allWarehouses as $wareKey => $wareInfo) {
				if (in_array($wareInfo['id'], $userWareInfo_new)) {
					$allWarehouses[$wareKey]['checked'] = 'true';
				}
			}
		}
		$ware_json = json_encode($allWarehouses);

		// 分配变量
		$this->smarty->assign('user_name', $user_info['user_name']);
		$this->smarty->assign('name', $user_info['real_name']);
		$this->smarty->assign('user_id', $set_user_id);

		$this->smarty->assign('allRole', $allRole);
		$this->smarty->assign('newUserRole', $newUserRole);

		$this->smarty->assign('cate_json', $cate_json);
		$this->smarty->assign('userCatesInfo_new', implode(',', $userCatesInfo_new));

		$this->smarty->assign('ware_json', $ware_json);
		$this->smarty->assign('userWareInfo_new', implode(',', $userWareInfo_new));

		$this->smarty->display('sys_user_authorization.tpl');
	}
	/**
	 * 设置用户权限
	 */
	public function setuserroleAction() {
		$getData = $this->getRequest()->getParams();
		$user_id = $getData['user_id'];
		$data = array();
		$data['roles'] = isset($getData['roles']) ? $getData['roles'] : array();
		$data['cates'] = explode(',', $getData['cates']);
		$data['wares'] = explode(',', $getData['wares']);
		$result_content = '';
		if ($user_id) {
			$res = $this->userModel->setUserPrivilige($user_id, $data, $this->_session->user_name);
			echo json_encode($res);
		} else {
			echo json_encode(array('status' => 0, 'content' => '修改失败，非法用户'));
		}
	}
	/**
	 * 同步用户
	 */
	public function syncAction() {
		$ssoHost = Helper_Config::get('base', 'sso', 'host');
		$uurl = $ssoHost . "/login/safelog/listusernameerp/";
		$data = array('1');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, $this->_session->user_name.":".$this->_session->password);//用户登录时候用户中心返回的用户名和密码
        curl_setopt($ch, CURLOPT_URL, $uurl );
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    //用于返回的原始输出
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data );  //传输内容
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        $response = curl_exec( $ch );
        curl_close( $ch );
        $users = @json_decode($response, true);
        $user_model = new Application_Model_User();
		$bench = array();
		foreach ($users['rs'] as $user) {
			// 不存在则添加
			if (!$user_model->exist($user['account'])) {
				$data = array();
				$data['user_name'] = $user['username'];
				$data['department_id'] = $user['department_id'];
				$data['real_name'] = $user['real_name'];
				$data['is_group_leader'] = $user['is_group_leader'];
				$data['is_enable'] = $user['is_lock'] ? 0 : 1;
				
				$bench[] = $data;
			}
		}
		$user_model->addUsers($bench);
		echo "success";
	}
}